package com.example.jt_common;

import org.csource.common.MyException;
import org.csource.fastdfs.*;

import java.io.*;

public class FileUtil {
    /**
     *
     * @param input 需要上传的文件byte[]
     * @param file_etx_name 需要上传的文件拓展名
     * @param trackservers trackservers地址
     * @return  返回服务器所在的地址 如：group1/M00/00/00/wKj9ClzmB7qAHzVOAABQPCW6nuk545.png
     */
    public String fileUpload(byte[] input,String file_etx_name,String trackservers){
        String result=null;
        try {
            System.out.println(trackservers);
            ClientGlobal.initByTrackers(trackservers);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

        try {
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            StorageServer storageServer = null;
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);

            String fileIds[] = storageClient.upload_file(input,file_etx_name,null);

            result = fileIds[0] +"/"+fileIds[1];

        } catch (IOException e) {

            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return result;
    }

}
